package com.example.back.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.back.dto.req.XdrugSave;
import com.example.back.dto.req.ZDrugDoDisableReq;
import com.example.back.dto.req.ZDrugListReq;
import com.example.back.dto.resp.XdrugInfo;
import com.example.back.dto.resp.XdrugInfoRsp;
import com.example.back.dto.resp.XdrugListRsp;
import com.example.back.feign.FileFeign;
import com.example.back.jpa.KeysRepository;
import com.example.back.jpa.XDrugBaseInfoRepository;
import com.example.back.mapper.XDrugMapper;
import com.example.back.service.XDrugService;
import com.example.common.entity.Keys;
import com.example.common.entity.XDrugBaseInfo;
import com.example.common.support.Result;
import com.example.common.utils.IdUtils;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.javers.spring.annotation.JaversAuditable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wxy
 * @Date 2021/1/15 13:48
 * @Version 1.0
 */
@Service
public class XDrugServiceImpl implements XDrugService {

    @Resource
    XDrugMapper xDrugMapper;

    @Resource
    XDrugBaseInfoRepository xDrugBaseInfoRepository;

    @Resource
    KeysRepository keysRepository;

    @Resource
    FileFeign fileFeign;
    /**
     * 西药列表
     */
    @Override
    public PageInfo<XdrugListRsp> List(ZDrugListReq zDrugListReq) {
        return PageHelper.startPage(zDrugListReq.getPage(),zDrugListReq.getLimit()).doSelectPageInfo(() -> xDrugMapper.List(zDrugListReq));
    }

    /**
     * 启用、禁用
     */
    @Override
    @JaversAuditable
    public Object DoDisable(ZDrugDoDisableReq zDrugDoDisableReq) {
        XDrugBaseInfo xDrugBaseInfo = xDrugBaseInfoRepository.findById(zDrugDoDisableReq.getDID()).orElse(null);
        if (xDrugBaseInfo.getDisable()){
            xDrugBaseInfo.setDisable(false);
            xDrugBaseInfoRepository.save(xDrugBaseInfo);
        }else {
            xDrugBaseInfo.setDisable(true);
            xDrugBaseInfoRepository.save(xDrugBaseInfo);
        }
        return null;
    }

    /**
     * 详情
     */
    @Override
    public XdrugInfoRsp Info(ZDrugDoDisableReq zDrugDoDisableReq) {
        XdrugInfoRsp xdrugInfoRsp = new XdrugInfoRsp();
        //用药方式
        Integer  typeWay = 2;
        List<Keys> ways = keysRepository.findByType(typeWay);

        //用药频率
        Integer  typeFreq = 4;
        List<Keys> freqs = keysRepository.findByType(typeFreq);

        XdrugInfo xdrugInfo = xDrugMapper.Info(zDrugDoDisableReq.getDID());
        xdrugInfoRsp.setXdrugInfo(xdrugInfo);
        xdrugInfoRsp.setFreq(freqs.stream().map(Keys::getName).toArray());
        xdrugInfoRsp.setWay(ways.stream().map(Keys::getName).toArray());
        return xdrugInfoRsp;
    }

    /**
     * 上传文件
     */
    @Override
    public HashMap<String,Object> UpLoad(MultipartFile file) {
        Result<Object> result = fileFeign.updateFile(file);        
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("UpSess",true);
        hashMap.put("url",result.getData());
        return hashMap;
    }

    /**
     *
     */
    @Override
    @JaversAuditable
    public Object Save(XdrugSave xdrugSave) {
        XDrugBaseInfo xDrugBaseInfo = xDrugBaseInfoRepository.findById(xdrugSave.getDrugID()).orElse(null);
        if (!ObjectUtils.isEmpty(xDrugBaseInfo)) {
            xDrugBaseInfo.setDoes(xdrugSave.getDoes());
            xDrugBaseInfo.setDoesUnit(xdrugSave.getDoesUnit());
            xDrugBaseInfo.setDrugId(xdrugSave.getDrugID());
            xDrugBaseInfo.setDrugName(xdrugSave.getDrugName());
            xDrugBaseInfo.setFreq(xdrugSave.getFreq());
            xDrugBaseInfo.setImg(xdrugSave.getImg());
            xDrugBaseInfo.setMedWay(xdrugSave.getMedWay());
            xDrugBaseInfo.setPy(xdrugSave.getPY());
            xDrugBaseInfo.setPrice(xdrugSave.getPrice());
            xDrugBaseInfo.setRemark(xdrugSave.getRemark());
            xDrugBaseInfo.setSpec(xdrugSave.getSpec());
            xDrugBaseInfo.setDrugType(xdrugSave.getDrugType());
            xDrugBaseInfoRepository.save(xDrugBaseInfo);
        }else {
            XDrugBaseInfo xDrugBaseInfo1 =new XDrugBaseInfo();
            xDrugBaseInfo1.setDrugId(IdUtils.generateId());
            xDrugBaseInfo1.setCreateTime(new Date());
            xDrugBaseInfo1.setDoes(xdrugSave.getDoes());
            xDrugBaseInfo1.setDoesUnit(xdrugSave.getDoesUnit());
            xDrugBaseInfo1.setDrugName(xdrugSave.getDrugName());
            xDrugBaseInfo1.setFreq(xdrugSave.getFreq());
            xDrugBaseInfo1.setImg(xdrugSave.getImg());
            xDrugBaseInfo1.setMedWay(xdrugSave.getMedWay());
            xDrugBaseInfo1.setPy(xdrugSave.getPY());
            xDrugBaseInfo1.setPrice(xdrugSave.getPrice());
            xDrugBaseInfo1.setRemark(xdrugSave.getRemark());
            xDrugBaseInfo1.setSpec(xdrugSave.getSpec());
            xDrugBaseInfo1.setDisable(false);
            xDrugBaseInfo1.setDrugType(xdrugSave.getDrugType());
            xDrugBaseInfoRepository.save(xDrugBaseInfo1);
        }
        return null;
    }
}
